package com.gxa.pingan.service.impl;

import com.gxa.pingan.common.ExceptionCode;
import com.gxa.pingan.exception.BusinessException;
import com.gxa.pingan.mapper.PointManagerMapper;
import com.gxa.pingan.mapper.UserMapper;
import com.gxa.pingan.pojo.PointManager;
import com.gxa.pingan.pojo.User;
import com.gxa.pingan.service.PointManagerService;
import com.gxa.pingan.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor = Throwable.class)
public class PointManagerServiceImpl implements PointManagerService {
    @Autowired
    private PointManagerMapper pointManagerMapper = null;

    @Autowired
    private UserMapper userMapper = null;
    @Override
    public void setPoint(PointManager pointManager) throws BusinessException {
        if (pointManager == null) {
            throw new BusinessException(ExceptionCode.PARAM_ERROR);
        }
        //第一次添加积分
        if (pointManager.getId() == null && !StringUtils.isEmpty(pointManager.getPhone())) {
            User user = userMapper.selectByPhone(pointManager.getPhone());
            if (user == null) {
                throw new BusinessException(ExceptionCode.NO_SUCH_USER);
            }
            PointManager byPhone = pointManagerMapper.selectByPhone(pointManager.getPhone());
            if (byPhone != null) {
                throw new BusinessException(ExceptionCode.NO_USERS);
            }
            pointManager.setPointCount(pointManager.getBalance());
            if (pointManager.getBalance() == null) {
                pointManager.setBalance(0L);
            }
            if (pointManager.getPointCount() == null) {
                pointManager.setPointCount(pointManager.getBalance());
            }
            Integer add = pointManagerMapper.add(pointManager);
            if (add == 0) {
                throw new BusinessException(ExceptionCode.FAILED_OPERATION);
            }
        } else {//第二次添加积分
            PointManager byId = pointManagerMapper.selectById(pointManager.getId());
            if (byId == null) {
                throw new BusinessException(ExceptionCode.NO_SUCH);
            }
            if (byId.getBalance() < pointManager.getBalance()) {
                byId.setPointCount(byId.getPointCount() + (pointManager.getBalance() - byId.getBalance()));
            }
            byId.setBalance(pointManager.getBalance());


            Integer update = pointManagerMapper.update(byId);
            if (update == 0) {
                throw new BusinessException(ExceptionCode.FAILED_OPERATION);
            }
        }

    }

    @Override
    public List<Map<String, Object>> getList(Integer page, Integer size, String condition) {
        if (page == null || page <= 0) {
            page = 1;
        }
        if (size == null || size <= 0) {
            size = 10;
        }
        Integer offset = (page - 1) * size;
        return pointManagerMapper.findByPage(offset,size,condition);
    }

    @Override
    public Integer getListCount(String condition) {
        if (StringUtils.isEmpty(condition)) {
            condition = null;
        }
        return pointManagerMapper.findByPage(null,null,condition).size();
    }

}
